package test.com.sms.selenium.singlepoint.util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class SpHRSkillUserListOneDetailPageUtil extends SeleniumPageUtil {
	public final static By HRUD_LINK_USER_CS = By.xpath("//*[@id='ctl44_UserListReport_ReportOutput']/table/tbody/tr[3]/td[1]/a");
	
	public final static By HRUD_TD_NAME = By.xpath("//*[@id='dvUserEdit']/table[1]/tbody/tr/td[1]/table/tbody/tr[1]/td[1]");
	public final static By HRUD_SPAN_USER_NAME = By.id("ctl44_lblUserName");
	public final static By HRUD_TH_PROFICIENCY = By.xpath("//*[@id='ctl44_ProficiencyLegend_ReportOutput']/table/tbody/tr[1]/th[1]");
	public final static By HRUD_TH_DESCRIPTION = By.xpath("//*[@id='ctl44_ProficiencyLegend_ReportOutput']/table/tbody/tr[1]/th[2]");
	public final static By HRUD_TH_DETAILED_DESCRIPTION = By.xpath("//*[@id='ctl44_ProficiencyLegend_ReportOutput']/table/tbody/tr[1]/th[3]");
	public final static By HRUD_TD_EMAIL_ADDRESS = By.xpath("//*[@id='dvUserEdit']/table[1]/tbody/tr/td[1]/table/tbody/tr[2]/td[1]");
	public final static By HRUD_LINK_EMAIL_ADDRESS = By.xpath("//*[@id='ctl44_lblEmailAddress']/a");
	
	public final static By HRUD_SPAN_HEADER = By.id("ctl44_UserSkillsReport_Header_lblHeader");
	public final static By HRUD_IMG_REPORT_PREFERENCES = By.id("ctl44_UserSkillsReport_Header_imgPreferences");
	public final static By HRUD_IMG_EXPORT = By.id("ctl44_UserSkillsReport_Header_imgExcelExport");
	public final static By HRUD_IMG_REFRESH = By.id("ctl44_UserSkillsReport_Header_imgRefresh");
	
	public final static By HRUD_TD_PAGE = By.xpath("//*[@id='dvUserEdit']/table[3]/tbody/tr/td[1]");
	public final static By HRUD_SPAN_PAGE_START_NO = By.id("ctl44_UserSkillsReport_PagingTop_lblPagingPageNumber");
	public final static By HRUD_TD_OF = By.xpath("//*[@id='dvUserEdit']/table[3]/tbody/tr/td[3]");
	public final static By HRUD_SPAN_PAGE_COUNT = By.id("ctl44_UserSkillsReport_PagingTop_lblPagingPageCount");
	
	public final static By HRUD_INPUT_FIRST = By.id("ctl44_UserSkillsReport_PagingTop_imgFirst");
	public final static By HRUD_INPUT_PREVIOUS = By.id("ctl44_UserSkillsReport_PagingTop_imgPrevious");
	public final static By HRUD_INPUT_NEXT = By.id("ctl44_UserSkillsReport_PagingTop_imgNext");
	public final static By HRUD_INPUT_LAST = By.id("ctl44_UserSkillsReport_PagingTop_imgLast");
	
	public final static By HRUD_IMG_FILTER_1 = By.xpath("//*[@id='ctl44_UserSkillsReport_ReportOutput']/table/tbody/tr[1]/th[1]/img");
	public final static By HRUD_IMG_FILTER_2 = By.xpath("//*[@id='ctl44_UserSkillsReport_ReportOutput']/table/tbody/tr[1]/th[2]/img");
	public final static By HRUD_IMG_FILTER_3 = By.xpath("//*[@id='ctl44_UserSkillsReport_ReportOutput']/table/tbody/tr[1]/th[3]/img");
	public final static By HRUD_IMG_FILTER_4 = By.xpath("//*[@id='ctl44_UserSkillsReport_ReportOutput']/table/tbody/tr[1]/th[4]/img");
	public final static By HRUD_TH_CATEGORY = By.xpath("//*[@id='ctl44_UserSkillsReport_ReportOutput']/table/tbody/tr[2]/th[1]");
	public final static By HRUD_TH_SUB_CATEGORY = By.xpath("//*[@id='ctl44_UserSkillsReport_ReportOutput']/table/tbody/tr[2]/th[2]");
	public final static By HRUD_TH_SKILL = By.xpath("//*[@id='ctl44_UserSkillsReport_ReportOutput']/table/tbody/tr[2]/th[3]");
	public final static By HRUD_TH_SUB_PROFICIENCY = By.xpath("//*[@id='ctl44_UserSkillsReport_ReportOutput']/table/tbody/tr[2]/th[4]");
	
	public static void enterUserListDetailPage(){
		SpHRSkillUserListPageUtil.enterHRSkillManagementUserListPage();
		WebElement csUser = SeleniumUtil.findElement(HRUD_LINK_USER_CS);
		SeleniumUtil.click(csUser);
		waitForPageToLoad();
		waitForResultRefresh();
	}
	public static void waitForPageToLoad() {
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.waitOnElements(new By[]{
				HRUD_IMG_REPORT_PREFERENCES, HRUD_IMG_EXPORT, HRUD_INPUT_FIRST, HRUD_INPUT_PREVIOUS, 
				HRUD_INPUT_NEXT, HRUD_INPUT_LAST
			});
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(HRUD_IMG_REPORT_PREFERENCES));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(HRUD_IMG_EXPORT));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(HRUD_INPUT_FIRST));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(HRUD_INPUT_PREVIOUS));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(HRUD_INPUT_NEXT));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(HRUD_INPUT_LAST));
		
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(HRUD_IMG_FILTER_1));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(HRUD_IMG_FILTER_2));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(HRUD_IMG_FILTER_3));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(HRUD_IMG_FILTER_4));
	}
	
	/**wait for result refresh, if time out 30 seconds not response return an exception*/
	public static void waitForResultRefresh(){
		long beforeWait = System.currentTimeMillis();
		long afterWait;
		boolean refreshEnd = SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(HRUD_IMG_REFRESH));
		
		while(refreshEnd){
			afterWait = System.currentTimeMillis();
			if((afterWait - beforeWait) > 30 * 1000){
				SeleniumUtil.fail("timeout 30 seconds not response");
				return;
			}else{
				waitForPageToLoad();
				refreshEnd = SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(HRUD_IMG_REFRESH));
			}
		}
	}
}
